#include "addfriendsender.h"
#include "ui_addfriendsender.h"
#include "user.h"
//#include "maininterfacewidget.h"
#include <QHostAddress>
#include <QMessageBox>
addfriendsender::addfriendsender(QWidget *parent,user userSelf) :
    QDialog(parent),
    ui(new Ui::addfriendsender)
{
    ui->setupUi(this);
    this->userSelf = userSelf;
    connect(ui->btn_send_addfriendsender,&QPushButton::clicked,this, &addfriendsender:: on_btn_send_addfriendsender_clicked);
    connect(ui->btn_clear_addfriendsender,&QPushButton::clicked,this, &addfriendsender:: on_btn_clear_addfriendsender_clicked);
}
void addfriendsender::on_btn_send_addfriendsender_clicked()
{
    //Sender创建
    UserBrief Sender;
    Sender = {
        userSelf.getUserId(),//id
        userSelf.getUserName(),//username
        "",//photo
        userSelf.getUserGender()//gender
    };
    //qDebug() << "sender信息:" << Sender.username + Sender.id;
    //get content
    QString Brief = ui->messageSendTextEdit->text();//传输服务器
    QString senderToken = userSelf.getUserPassword();
    QString receiverId = ui->receiverId->text();
    receiverId.toLongLong();//id转化为long long
    //sender信息
    QJsonObject senderValue{
        {"id", Sender.id.toInt()},
        {"username", Sender.username},
        {"photo", ""},
        {"gender", Sender.gender.toInt()}
    };
    qDebug()<<"SenderValue"<<senderValue;
    QNetworkRequest request(QUrl("http://localhost:19999/slimarveous/add_friend"));
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json2");
    //QJsonObject json2;
    QJsonObject json2{
        {"token",senderToken},
        {"sender",senderValue},
        {"receiver_id",receiverId.toInt()},
        {"brief", Brief}
    };
    QJsonDocument doc(json2);
    //qDebug()<<"json"<<json2;
    //QJsonDocument doc(json2);
    QByteArray data = doc.toJson(QJsonDocument::Compact);
    QNetworkReply *reply = manager.post(request, data);
    //qDebug()<<"data"<<data;
    connect(reply, &QNetworkReply::finished, [=]()
    {
        if (reply->error() == QNetworkReply::NoError)
        {
            // 读取响应数据
            QByteArray dataReply = reply->readAll();
            // 将 JSON 数据转换为 QVariant 对象
            QJsonParseError error;
            QJsonObject result = QJsonDocument::fromJson(dataReply, &error).object();
            //qDebug()<<result;
            // 处理 JSON 数据
            if(error.error == QJsonParseError::NoError)
            {
                QVariantMap resultMap = result.toVariantMap();
                //提取状态码
                QVariant status_code = resultMap.value("status_code");
                status_code.toString();
            }
        }
        else
            qDebug()<<"error";
        reply->deleteLater();
    });
    this->close();
    QMessageBox:: information(this, "好友请求","你好，"+userSelf.getUserName()+"! "+"你已向用户id为：" +receiverId+"的好友发送请求！");
 //跳转到主界面
}


void addfriendsender::on_btn_clear_addfriendsender_clicked()
{
    ui->messageSendTextEdit->clear();
    ui->receiverId->clear();
}


addfriendsender::~addfriendsender()
{
    delete ui;
}
